GBML manual / Language

Expressions and statements

The operators, control flow and the with statement that let one line of code reach into another instance.

Operators

Arithmetic works as you would expect, plus two spellings for the operators that read better as words: div and mod alongside / and %. Comparisons, and/or/not alongside &&/||/ !, bitwise operators, the null-check pair ??/??=, the ternary cond ? a : b, ++/--, and compound assignment (+=, -=, and so on) all work the ordinary way.

var hp_pct = hp / 100.0;
if (hp <= 0 and not is_dead) { begin_death(); }
shield_charge ??= 0;          // only assigns if shield_charge is currently nothing/unset
lives = lives > 0 ? lives - 1 : 0;

/ between two whole numbers throws the fraction away

Read that first line again. 100.0, not 100 - and that single character is the whole reason this section exists.

/ with an int on both sides is a whole-number divide, as it is in C++ and in the hardware underneath. hp / 100 with hp at 37 is 0, not 0.37. Nothing is rounded and nothing complains; the fraction simply never exists.

That is correct and wanted about half the time - halving a pixel count, turning a tick count into a band, finding a midpoint:

var half_w = room_width() / 2;   // a pixel count halved. The lost half-pixel IS the point.
var band   = ticks / 8;          // which eight-frame band we are in. A whole number by nature.

It is a bug the other half of the time, and nearly always in the same place: an interpolation.

// Wrong. `timer` and 40 are both whole numbers, so `t` is 0 for thirty-nine frames
// and 1 on the fortieth - the title does not slide in, it appears.
var t = timer / 40;
y = 160 * (1 - t);

The symptom is not a crash, and not a visibly wrong number. It is an animation that snaps instead of easing, which reads as a missing tween, a wrong frame count, or nothing much at all

ported game shipped exactly this way and were found by reading the generated C++, not by watching the game.

Saying which one you meant

One character fixes it. Make either side real and the whole expression becomes a real divide:

var t = timer / 40.0;            // 0.025, 0.05, 0.075 ... as intended

And when you did mean whole-number division, say so with div, which means nothing else:

var band = ticks div 8;          // unmistakable, and identical code underneath

The compiler warns when it can see a whole-number quotient being used as a fraction - clamped at 1, subtracted from 1, or scaled by a real. It deliberately cannot see every case: room_width() / 2 feeding a position is correct and looks the same from the outside, so take the warning as a useful catch rather than a complete one. div is never warned about. Writing it is how you say you meant it.

Statements

if/else, switch/case/default, for, while, do … until, repeat, break, continue, exit, return - all ordinary control flow. do … until and repeat are the two that do not exist in every language: do { … } until (cond) runs the body first and re-checks after, the mirror image of while; repeat (n) { … } runs the body exactly n times with no loop variable to manage yourself.

A switch looks at a whole number (or a named one, like a member of an enum), and nothing else: a switch on a piece of text is refused. Use if / else if with == to choose by text: if (name == "a") { … } else if (name == "b") { … } else { … }.

for has one small kindness: every statement in GBML ends with ;, so ending the counting step the same way - for (var i = 0; i < n; i++;) - is accepted rather than marked wrong, though for (var i = 0; i < n; i++) stays the form to learn and formatting the code writes the extra ; back out.

A for can move two counters at once: the first part and the last part of its header take a list separated by commas, for (var i = 0, j = 9; i < j; i++, j--). The pieces run in the order written. A comma means "and" only in those two places: in the middle part (the test) it is refused, and everywhere else it separates the items of a call or a list, as ever.

var n = 3;
repeat (n) {
    instance_create(x, y, obj_spark);
}

with: running code as if you were another instance

with (target) { … } runs its body once per instance target refers to, with self switched to mean that instance for the duration. target can be a single instance reference, an object (meaning every instance of it, and of its children), or all (every instance that exists). Inside the block, other still refers to whoever was self before the with started - which is how an alarm on a turret can reach into the instance it just spawned without losing track of the turret itself:

with (instance_create(x, y, obj_bolt)) {
    direction = dir_to(x, y, other.target.x, other.target.y);
    speed     = 2;
}

Here, inside the block, x/y/direction/speed all belong to the new obj_bolt instance (that is self now); other.target reaches back out to the turret's own target variable. Objects and instances covers self/other in full, including what they mean in a Collision event, where the same two names refer to something else again.

A with block can leave the whole event or script it is written in, with return (and a value, in a script that gives one back) or with exit. The sweep stops on the spot: the instances the block had not reached yet are not visited at all, not even to be skipped. So a script that looks for the first enemy that is nearly dead, and returns it, costs only as many steps as it took to find one:

with (obj_enemy) {
    if (hp <= 1) {
        return self;
    }
}
return nothing;

This holds for a block that visits one kind of object, a kind and its children, all, and blocks inside blocks (a return two blocks deep stops both). A block with no return or exit in it visits every instance, as always.

Accessors

array[i]/list[i] and struct.field work as plain indexing/field access. The bounded grid type indexes with TWO numbers instead, grid[# x, y] - row-major, x (the column) first, then y (the row). Leave a space after the # ([# x, y], not [#x, y]) - written with no space before a digit or a letter a-f, #1 or #a reads as the START of a #RRGGBB color literal instead, and the compiler reports it as one. A grid needs its own two-number capacity to make that indexing possible - see Variables, types and scope for /// @capacity WxH.

The bounded map type does not index with [? key], even though the ? still parses - map_get/map_set replace it safely, since every container here is bounded and a missing or full map is a real, expected case. Neither does stack/queue/set index at all, by design - each is reached only through its own named functions. See Accessors for the full story of which container gets which syntax, and why.

Pre-processing

#macro NAME value defines a compile-time constant substituted at every use - useful for a number you want named but do not want to spend an instance variable on. #region/#endregion mark a foldable section in the editor and do nothing else. enum Name { A, B, C } declares a small closed set of named integer values, the ordinary way.

Comments

// and /* */ are ignored by the compiler entirely. /// is not - a documentation comment directly above a function, parameter, or variable's first assignment is read by the type checker itself (Variables, types and scope), not just displayed somewhere.

See also: Variables, types and scopeFunctionsObjects and instancesAccessors